<html>
    <head>
        <style type="text/css">
            .btn
            {
                background-color:#FFF5EE;
                cursor:pointer;
                align: right;
            }
        </style>
    </head>
</html>
<?php
include 'conn.php';
$ActivityId = $_REQUEST['selectedActivity'];
$_SESSION['selectedActivity'] = $ActivityId;
if (!empty($_SESSION['userType'])) {

    $userType = $_SESSION['userType'];
}
if ($userType == 'System Admin') {
    $centreId = intval(isset($_POST['submit']) ? $_POST['ddlCentre'] : '' );
} else {
    include 'conn.php';
    $qry = "Select CentreId from Centre where CentreName='" . $_SESSION['centreName'] . "'";
    $result = mysql_query($qry);
    while ($row = mysql_fetch_array($result)) {
        $centreId = intval($row['CentreId']);
    }
}


if ($centreId == 0) {
    $clwhere = "client.CentreId is not null";
    $where = "CentreId is not null";
    $kwhere = "INNER JOIN centre on client.CentreId= centre.CentreId where client.CentreId is not null";
} else {
    $clwhere = "client.CentreId=$centreId";
    $where = "CentreId=$centreId";
    $kwhere = "INNER JOIN centre on client.CentreId=centre.CentreId where client.CentreId=$centreId";
}
$startDate = date('Y-m-d', strtotime($_REQUEST['startDate']));
$endDate = date('Y-m-d', strtotime($_REQUEST['endDate']));
$_SESSION["startDate"] = $_REQUEST['startDate'];
$_SESSION["endDate"] = $_REQUEST['endDate'];
$_SESSION["centreId"] = $centreId;
if ($userType != "Therapy Assistant") {
    echo"

        <form action='Reports/activityexport.php' method='post'>
        <p><input type='submit'button class='btn' id='btnExport'value='Export'onClick='window.location.href='Reports/activityexport.php'

        ></p></form>";
}

$actsql = "Select ActivityName from activity where ActivityId='$ActivityId'";
$resultactsql = mysql_query($actsql);
while ($row = mysql_fetch_array($resultactsql)) {
    $ActivityName = $row['ActivityName'];
}
    echo "
<div class='tablereport' id='table'style='width:980px;overflow:scroll'>
			<table id = 'tableExcel' cellspacing='2' cellpadding='2' align='center' name='tableatt'   >   
                        <tr>
			<td>
			S/No</td>
			<td>
			NRIC</td>
			<td>
			Name</td>";
    if ($userType == "System Admin") {
        echo "
                        <td>Centre Name</td>";
    }
    echo "
                            <td>
			Age</td>";
    include 'conn.php';
    $checkDateSNo = $startDate;
    $count = 1;

    while ($checkDateSNo <= $endDate) {
        $sqlhols = "Select count(*) from holiday where Date='$checkDateSNo'";
        $resulthols = mysql_query($sqlhols);
        while ($row = mysql_fetch_array($resulthols)) {
            if ($row['count(*)'] == 0) {
                $day = date('l', strtotime($checkDateSNo));
                if ($day != "Sunday" && $day != "Saturday") {
                    echo "<td>" . $count . "</td>";
                    $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
                    $count++;
                } else {
                    $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
                }
            } else {
                $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
            }
        }
    }
    echo "<td>
			<font face='Arial, Helvetica, sans-serif'>Average Performance</font></td>
                  <td>
			<font face='Arial, Helvetica, sans-serif'>Total Attendance</font></td></tr>";
    $ttlattendance = 0;
    include 'conn.php';
    $sql = "SELECT * from Client $kwhere";
    $result = mysql_query($sql);
    $sno = 1;
    $age = 0;
    while ($row = mysql_fetch_array($result)) {
        echo "
			<tr>
			<td>
			" . $sno . "</td>
			<td>
			" . $row['NRIC'] . "</td>
			<td>
			" . $row['CustomerName'] . "</td>";
        if ($userType == "System Admin") {
            echo "
                       <td>
			" . $row['CentreName'] . "</td>";
        }
        $age = date('Y-m-d') - $row['DateOfBirth'];
        echo "
			<td>
			" . $age . "</td>";
        $checkDate = $startDate;
        $workingday = 0;
        $attendance = 0;
        $totalPerformance = 0;
        while ($checkDate <= $endDate) {
            $sqlhols1 = "Select count(*) from holiday where Date='$checkDate'";
            $resulthols1 = mysql_query($sqlhols1);
            while ($rowhol = mysql_fetch_array($resulthols1)) {
                if ($rowhol['count(*)'] == 0) {
                    $day = date('l', strtotime($checkDate));
                    if ($day != 'Sunday' && $day != 'Saturday') {
                        $sqlattendance = "Select * from activityrecord where date(Date)='$checkDate' and ActivityId='$ActivityId' and NRIC='" . $row['NRIC'] . "' and $where";
                        $checkDate = date('Y-m-d', strtotime('+1 day', strtotime($checkDate)));
                        $workingday++;
                        $resultattendance = mysql_query($sqlattendance);
                        if (mysql_num_rows($resultattendance) == 1) {
                            $row1 = mysql_fetch_array($resultattendance);
                            $performance = $row1['Performance'];

                            echo "<td>$performance</td>";
                            $attendance++;
                            $totalPerformance += $performance;
                        } else {
                            echo "<td> - </td>";
                        }
                    }
                    else
                        $checkDate = date('Y-m-d', strtotime('+1 day', strtotime($checkDate)));
                } else {
                    $checkDate = date('Y-m-d', strtotime('+1 day', strtotime($checkDate)));
                }
            }
        }
        $ttlattendance += $attendance;
        if ($attendance == 0) {
            $averagePerformance = 0;
        } else {
            $averagePerformance = round($totalPerformance / $attendance, 5);
        }
        $avg = round($ttlattendance / $sno, 5);
        echo "<td>
			" . $averagePerformance . "
			</td>
                        <td>
			" . $attendance . "
			</td>";
        echo"</tr>";
        $sno++;
    }
    echo "
		</table></div><p>
                </p>
                <h4>Total Attendance for " . $ActivityName . " : " . $ttlattendance . "</h4><br>
                <h4>Average Attendance for " . $ActivityName . " : " . $avg . "</h4>    
                <br>";
?>